<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="depts" default-expand-all :expand-on-click-node="false" :props="defaultProps">
        <template #default="{data}">
          <el-row type="flex" align="middle" style="width: 100%; height: 40px;" justify="space-between">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="manager">{{ data.managerName }}</span>
              <el-dropdown @command="operateDept($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 部门操作 -->
    <add-dept ref="addDept" :current-node-id="currentNodeId" :show-dialog.sync="showDialog" @updateDeparment="'getDeparment'" />
  </div>
</template>

<script>
import { getDepartment, delDeparment } from '@/api/department'
import { transListToTresData } from '@/utils/index'
import addDept from './add-dept.vue'
export default {
  name: 'Department',
  components: {
    addDept
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children', // 子节点的属性名
        label: 'name' // 标签节点的属性名
      },
      showDialog: false, // 控制弹层
      currentNodeId: null // 存储当前点击的id
    }
  },
  created() {
    this.getDepartment() // 重新渲染
  },
  methods: {
    // 封装好方法
    async  getDepartment() {
      const result = await getDepartment()
      // console.log(result)
      this.depts = transListToTresData(result, 0) // 调用方法 传入起始值
    },
    // 操作部门方法
    operateDept(type, id) {
      // 添加部门
      if (type === 'add') {
        // 显示弹层
        this.showDialog = true
        // 记录id
        this.currentNodeId = id

        // 编辑部门
      } else if (type === 'edit') {
        // 调用获取部门详情的methods(子组件)
        // console.log(this.$refs.addDept)
        // 问题:获取时机问题 ->prop 更新异步操作
        // $nextTick() {是做了promise加上setTimeout的封装}  批量更新 ->将update的操作放到了里面
        console.log(this.$refs.addDept)
        this.currentNodeId = id
        this.showDialog = true // 显示弹层
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDeparment(id)
          this.getDepartment() // 重新渲染
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }
    }
  }
}

</script>

<style lang="scss" scoped>
.app-container{
padding: 30px 149px;
font-size: 14px;
}
.manager{
  font-size: 14px;
  margin-right: 10px;
}

</style>
